package com.hliushi.mq.batch;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 批量发送同步消息
 *
 * @author llhuang10
 * @date 2021/8/11 16:04
 */
public class BatchSyncProducer {


    public static void main(String[] args) {
        // 1.创建消息生产者producer, 并指定生产者组名
        DefaultMQProducer producer = new DefaultMQProducer("group1");
        // 2.指定Nameserver地址
        producer.setNamesrvAddr("cvm-node01:9876;cvm-node02:9876");
        // 3.发送消息
        try {
            producer.start();

            List<Message> messages = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                // 4.创建消息, 指定主题Topic, Tag和消费题
                Message message = new Message("base", "BatchTag", ("hello world" + i).getBytes());

                messages.add(message);

                // 线程睡一秒
                TimeUnit.SECONDS.sleep(1);
            }
            // 5.发送消息
            SendResult result = producer.send(messages);

            // 发送结果 = SendResult [sendStatus=SEND_OK, msgId=00000000000000000000000000000001000018B4AAC2380538420000,
            // 00000000000000000000000000000001000018B4AAC2380538420001,
            // 00000000000000000000000000000001000018B4AAC2380538420002,
            // 00000000000000000000000000000001000018B4AAC2380538420003,
            // 00000000000000000000000000000001000018B4AAC2380538420004,
            // 00000000000000000000000000000001000018B4AAC2380538420005,
            // 00000000000000000000000000000001000018B4AAC2380538420006,
            // 00000000000000000000000000000001000018B4AAC2380538420007,
            // 00000000000000000000000000000001000018B4AAC2380538420008,
            // 00000000000000000000000000000001000018B4AAC2380538420009,
            // offsetMsgId=010F9AE900002A9F0000000000005D38,
            // 010F9AE900002A9F0000000000005DFD,
            // 010F9AE900002A9F0000000000005EC2,
            // 010F9AE900002A9F0000000000005F87,
            // 010F9AE900002A9F000000000000604C,
            // 010F9AE900002A9F0000000000006111,
            // 010F9AE900002A9F00000000000061D6,
            // 010F9AE900002A9F000000000000629B,
            // 010F9AE900002A9F0000000000006360,
            // 010F9AE900002A9F0000000000006425,
            // messageQueue=MessageQueue
            // [topic=base, brokerName=broker-a, queueId=1], queueOffset=19]
            System.out.println("发送结果 = " + result);
        } catch (MQClientException | InterruptedException | RemotingException | MQBrokerException e) {
            e.printStackTrace();
        } finally {
            // 6.关闭生产者producer
            producer.shutdown();
            System.out.println("发送成功!");
        }
    }
}
